﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework;
using System;
using System.Collections.Generic;

namespace ProjectMG
{
    public static class AssetUtility
    {
        private static readonly Dictionary<AssetKey, string> s_AssetNameCache = new Dictionary<AssetKey, string>();

        public static string GetFontAsset(string assetName)
        {
            return Utility.Text.Format("Assets/GameMain/Fonts/{0}.ttf", assetName);
        }

        public static string GetSceneAsset(string assetName)
        {
            return Utility.Text.Format("Assets/GameMain/Scenes/{0}.unity", assetName);
        }

        public static string GetMusicAsset(string assetName)
        {
            return Utility.Text.Format("Assets/GameMain/Music/{0}.mp3", assetName);
        }

        public static string GetSoundAsset(string assetName)
        {
            return Utility.Text.Format("Assets/GameMain/Sounds/{0}.wav", assetName);
        }

        public static string GetEntityAsset(string assetName)
        {
            return Utility.Text.Format("Assets/GameMain/Entities/{0}.prefab", assetName);
        }

        public static string GetUIFormAsset(string assetName)
        {
            return Utility.Text.Format("Assets/GameMain/UI/UIForms/{0}.prefab", assetName);
        }

        public static string GetUISoundAsset(string assetName)
        {
            return Utility.Text.Format("Assets/GameMain/UI/UISounds/{0}.wav", assetName);
        }

        public static string GetBinaryDictionaryAsset()
        {
            return Utility.Text.Format("Assets/GameMain/Localization/{0}/Dictionaries/Dictionary.bytes", GameEntry.Localization.Language);
        }

        public static string GetDictionaryListAsset()
        {
            return Utility.Text.Format("Assets/GameMain/Localization/{0}/Dictionaries/DictionaryList.txt", GameEntry.Localization.Language);
        }

        public static string GetConfigListAsset()
        {
            return "Assets/GameMain/Configs/ConfigList.txt";
        }

        public static string GetBinaryConfigAsset()
        {
            return "Assets/GameMain/Configs/DefaultConfig.bytes";
        }

        public static string GetDataTableAsset(string assetName)
        {
            string assetFullName = null;
            AssetKey assetKey = new AssetKey(AssetType.DataTable, assetName);
            if (!s_AssetNameCache.TryGetValue(assetKey, out assetFullName))
            {
                assetFullName = Utility.Text.Format("Assets/GameMain/DataTables/{0}.{1}", assetName, GameEntry.BuiltinData.UseDataTableBytes ? "bytes" : "txt");
                s_AssetNameCache.Add(assetKey, assetFullName);
            }

            return assetFullName;
        }

        public static string GetConfigAsset(string assetName)
        {
            string assetFullName = null;
            AssetKey assetKey = new AssetKey(AssetType.Config, assetName);
            if (!s_AssetNameCache.TryGetValue(assetKey, out assetFullName))
            {
                assetFullName = Utility.Text.Format("Assets/GameMain/Configs/{0}.txt", assetName);
                s_AssetNameCache.Add(assetKey, assetFullName);
            }

            return assetFullName;
        }

        public static string GetDictionaryAsset(string assetName)
        {
            string assetFullName = null;
            AssetKey assetKey = new AssetKey(AssetType.Dictionary, assetName);
            if (!s_AssetNameCache.TryGetValue(assetKey, out assetFullName))
            {
                assetFullName = Utility.Text.Format("Assets/GameMain/Localization/{0}/Dictionaries/{1}.txt", GameEntry.Localization.Language, assetName);
                s_AssetNameCache.Add(assetKey, assetFullName);
            }

            return assetFullName;
        }

        private enum AssetType
        {
            Unknown = 0,
            Character, // 角色
            Config, // 配置
            DataTable, // 数据表
            Dictionary, // 字典
            Effect, // 特效
            Font, // 字体
            Movie, // 视频
            Scene, // 场景
            Shader, // Shader
            UIAtlas, // UI图集
            UIEffect, // UI特效
            UIForm, // UI
            UITexture, // UI底图
        };

        private struct AssetKey : IEquatable<AssetKey>
        {
            public readonly AssetType Type;
            public readonly string Name;

            public AssetKey(AssetType type, string name)
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new GameLogicException("Name is invalid.");
                }

                Type = type;
                Name = name;
            }

            public bool Equals(AssetKey other)
            {
                return Type == other.Type && Name == other.Name;
            }

            public override int GetHashCode()
            {
                return (int)Type ^ Name.GetHashCode();
            }
        }
    }
}
